<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <jsp:include page="../include.jsp" flush="true"/>
	
	<script type="text/javascript">
		var win;
		var url;
		$(function() {
			$('#btn-save,#btn-cancel').linkbutton();
			win = $('#user-window').window( {
				collapsible : false,//折叠
				minimizable : false,//最小化
				maximizable : false,//最大化
				draggable : true,//拖动
				resizable : false,//改变大小
				modal : true,
				closed : true
			});
		});
		$(function(){
			if(user_role_id != '5'){
				alert('您无权操作！');
				$('div').remove();
				$('body').append('<h1>您无权操作!</h1>');
				return false;
			}
		});
		/////////////////////////////////////////////////
		function append(tabname) {
			reject();
			setWinAttr(tabname);
			if(tabname == 'faculty'){
				$("#idField").attr({readonly:"readonly", onblur:"", title:"由系统设置"});
				url ='a04!addFaculty?user_role_id='+user_role_id;
			}else if(tabname == 'major'){
				$("#idField").attr({readonly:"readonly", onblur:"", title:"由系统设置"});
				url ='a04!addMajor?user_role_id='+user_role_id;
			}else if(tabname == 'datatype'){
				$("#idField").attr({readonly:false, onblur:"test()", title:"数据格式"});
				url ='a04!addDatatype?user_role_id='+user_role_id;
			}else{
			}
			win.window('open');
			status = 'enable';
			$('#userform').form('clear');
			if($("#idField").attr("name") == 'datatype_id'){
				$("#idField").focus();
				test();
			}
		}
		function remove(tabname) {
			var rows = $('#tab_'+tabname).datagrid('getSelected');
			if(!rows){
				$.messager.show( {
						title : '警告',
						msg : '请先选择一行数据。'
				})
				return false;
			}
			var nameField;
			if(tabname == 'faculty') {
				nameField = rows.faculty_name;
				url ='a04!removeFaculty?user_role_id='+user_role_id+'&faculty_id='+rows.faculty_id;
			} else if(tabname == 'major'){
				nameField = rows.major_name;
				url ='a04!removeMajor?user_role_id='+user_role_id+'&major_id='+rows.major_id;
			} else if(tabname == 'datatype'){
				nameField = rows.datatype_name;
				url ='a04!removeDatatype?user_role_id='+user_role_id+'&datatype_id='+rows.datatype_id;
			} else{
				return false;
			}
			$.messager.confirm('提示','确定删除吗?',function(result){  
	            if (result){  
	                $.post(url,function(message){
	                    $('#tab_'+tabname).datagrid('reload');
	                    if(message.returnMsg == undefined){
	                    	$.messager.show({
		                    	title:'提示',
		                    	msg:'删除失败'
		                    });
	                    }else{
	                    	$.messager.show({
		                    	title:'提示',
		                    	msg:message.returnMsg
		                    });
	                    }
	                });
	            }
	            reject();
	        });
		}
		function update(tabname) {
			var row = $('#tab_'+tabname).datagrid('getSelected');
			if(!row){
				$.messager.show( {
						title : '警告',
						msg : '请先选择一行数据。'
				})
				return false;
			}
			if(tabname == 'faculty') {
				url ='a04!updateFaculty?user_role_id='+user_role_id;
			} else if(tabname == 'major'){
				url ='a04!updateMajor?user_role_id='+user_role_id;
			} else if(tabname == 'datatype'){
				url ='a04!updateDatatype?user_role_id='+user_role_id;
			} else{
				return false;
			}
			$("#idField").attr({readonly:"readonly", onblur:""});
			win.window('open');
			$('#userform').form('clear');
			$('#userform').form('load', row);
		}
		function save() {
			var m = url.split("?")[0].lastIndexOf("Major");
			var f = url.split("?")[0].lastIndexOf("Faculty");
			var d = url.split("?")[0].lastIndexOf("Datatype");
			$('#userform').form('submit', {
				url : url,
				onSubmit : function() {
					return $(this).form('validate');
				},
				success : function(message) {
					if(m == -1 && f == -1 && d > 0){
						$('#tab_datatype').datagrid('reload');
					}else if(m == -1 && f > 0 && d == -1){
						$('#tab_faculty').datagrid('reload');
					}else if(m > 0 && f == -1 && d == -1){
						$('#tab_major').datagrid('reload');
					}
					win.window('close');
					$.messager.show( {
						title : '提示',
						msg : message.returnMsg
					});
				}
			});
		}
		function reject(){
			$('.easyui-datagrid').datagrid('clearSelections');
		} 
		
		function closeWindow() {
			win.window('close');
			reject();
		}
		function setWinAttr(attrName){
			$("p").remove();
			$("#idField").attr("name",attrName+"_id");
			$("#nameField").attr("name",attrName+"_name");
		}
		function test(){
			var datatype_id = $("#idField").val();
			$.post("a04!testTypeId?datatype_id="+datatype_id,function(data){
				$("p").remove();
			    $("#nameField").before("<p>"+data.returnMsg+"</p>");
			    if (data.returnFlag == 'N') {
					$("#idField").focus();
					return false;
				}
			});
		}
	</script>
  </head>
  
  <body>
    <div class="easyui-tabs" data-options="fit:true,border:false,plain:true" tabPosition=''>
		<div title="帮助" data-options="fit:true" style="padding:10px">
			<menu>
				<li>学院字典管理</li>
				<li>专业字典管理</li>
				<li>数据类型字典管理 : 主键为数据的格式-必填</li>
			</menu>
		</div>
		<div title="学院管理" style="padding:5px">
			<table id="tab_faculty" class="easyui-datagrid"
					data-options="url:'a04!queryFaculty?user_role_id='+<%=session.getAttribute("user_role_id") %>,
						singleSelect:true,
						fit:true,
						fitColumns:true,
						rownumbers:true,
						toolbar:toolbarF,
						title:'学院管理',
						onSelect:function(rowIndex, rowData){
							reject();
							setWinAttr('faculty');
						}
					">
				<thead>
					<tr>
						<th data-options="field:'faculty_id'" width="80">ID</th>
						<th data-options="field:'faculty_name'" width="100">学院</th>
					</tr>
				</thead>
			</table>
		</div>
		
		<div title="专业管理" style="padding:5px">
			<table id="tab_major" class="easyui-datagrid"
					data-options="url:'a04!queryMajor?user_role_id='+<%=session.getAttribute("user_role_id") %>,
					singleSelect:true,
					fit:true,
					fitColumns:true,
					rownumbers:true,
					toolbar:toolbarM,
					title:'专业管理',
					onSelect:function(rowIndex, rowData){
						reject();
						setWinAttr('major');
					}
				">
				<thead>
					<tr>
						<th data-options="field:'major_id'" width="80">ID</th>
						<th data-options="field:'major_name'" width="100">专业</th>
					</tr>
				</thead>
			</table>
		</div>
		
		<div title="数据类型管理" style="padding:5px">
			<table id="tab_datatype" class="easyui-datagrid"
					data-options="url:'a04!queryDatatype?user_role_id='+<%=session.getAttribute("user_role_id") %>,
						singleSelect:true,
						fit:true,
						fitColumns:true,
						rownumbers:true,
						toolbar:toolbarT,
						title:'数据类型管理',
						onSelect:function(rowIndex, rowData){
							reject();
							setWinAttr('datatype');
						}
					">
				<thead>
					<tr>
						<th data-options="field:'datatype_id'" width="80">ID</th>
						<th data-options="field:'datatype_name'" width="100">数据类型</th>
					</tr>
				</thead>
			</table>
		</div>
	</div>
    <!-- ---------------------------------------------------------- -->
    <div id="user-window" title="用户窗口" style="width:450px;height:auto;">
		<div style="padding:20px 20px 40px 80px;">
			<form id="userform" method="post">
				<table>
					<tr>
						<td>字典主键:</td>
						<td>
							<input type="text" class="easyui-validatebox" name="" id="idField" onblur="test()" title="">
        				</td>
					</tr>
					
					<tr>
						<td>字典值:</td>
						<td>
							<input type="text" class="easyui-validatebox" name="" id="nameField" required=true>
						</td>
					</tr>
				</table>
			</form>
		</div>
		<div style="text-align:center;padding:5px;">
			<a href="javascript:void(0)" onclick="save()" id="btn-save" icon="icon-save">确认</a>
			<a href="javascript:void(0)" class="easyui-linkbutton" onclick="closeWindow()" id="btn-cancel" icon="icon-cancel">取消</a>
		</div>
	</div>
	<!-- ======================================================== -->
	<script type="text/javascript" src="./jquery-easyui-1.3.2/easyloader.js"></script>
	<script type="text/javascript">
		var user_role_id = <%=session.getAttribute("user_role_id")%>
		if(user_role_id == 1 || user_role_id == 5){
			var toolbarF = [{
				id:'addbtn',
				text:'增加',
				iconCls:'icon-add',
				handler:function(){append('faculty')}
			},{
				id:'updatebtn',
				text:'修改',
				iconCls:'icon-edit',
				handler:function(){update('faculty')}
			},{
				text:'删除',
				iconCls:'icon-cut',
				handler:function(){remove('faculty')}
			},{
				text:'返回',
				iconCls:'icon-undo',
				handler:function(){reject()}
			},{
				text:'帮助',
				iconCls:'icon-help',
				handler:function(){reject()}
			}];
			///////
			var toolbarM = [{
				id:'addbtn',
				text:'增加',
				iconCls:'icon-add',
				handler:function(){append('major')}
			},{
				id:'updatebtn',
				text:'修改',
				iconCls:'icon-edit',
				handler:function(){update('major')}
			},{
				text:'删除',
				iconCls:'icon-cut',
				handler:function(){remove('major')}
			},{
				text:'返回',
				iconCls:'icon-undo',
				handler:function(){reject()}
			},{
				text:'帮助',
				iconCls:'icon-help',
				handler:function(){reject()}
			}];
			///////
			var toolbarT = [{
				id:'addbtn',
				text:'增加',
				iconCls:'icon-add',
				handler:function(){append('datatype')}
			},{
				id:'updatebtn',
				text:'修改',
				iconCls:'icon-edit',
				handler:function(){update('datatype')}
			},{
				text:'删除',
				iconCls:'icon-cut',
				handler:function(){remove('datatype')}
			},{
				text:'返回',
				iconCls:'icon-undo',
				handler:function(){reject()}
			},{
				text:'帮助',
				iconCls:'icon-help',
				handler:function(){reject()}
			}];
		}
	</script>

  </body>
</html>
